package io.shallwe.scanner.auto;

import java.io.IOException;
import org.tio.server.ServerTioConfig;
import org.tio.server.TioServer;
import org.tio.server.intf.ServerAioHandler;
import org.tio.server.intf.ServerAioListener;

import com.jfinal.kit.LogKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.IPlugin;

/**
 *   自动扫描枪服务器启动器
 * @author 王小伟
 * @date 2016-06-16 00:23:11
 */
public class AutoScannerStarter  implements IPlugin {
	private int serverPort;
	private TioServer tioServer;
	public static ServerTioConfig groupContext;
	
	public  AutoScannerStarter() {
		PropKit.use("config.properties").appendIfExists("config-pro.properties");
		serverPort = 9004;
	}

	@Override
	public boolean start() {
		ServerAioHandler aioHandler = new AutoScannerHandler();
		ServerAioListener aioListener = new AutoScannerListener();
		groupContext = new ServerTioConfig("自动扫描枪消息接收服务端", aioHandler, aioListener);
		tioServer = new TioServer(groupContext);
		String ip=PropKit.get("serverIP","172.30.31.50");
		try {
			tioServer.start(ip, serverPort);
			return true;
		} catch (IOException e) {
			LogKit.error("开启消息端口失败！");
			throw new RuntimeException("开启消息端口失败！");
		}
	}
	
	@Override
	public boolean stop() {
		if (tioServer != null) {
			tioServer.stop();
		}
		return true;
	}
}
